package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductCategoryValue;
import com.erp.gather.domain.bo.ErpCollectProductCategoryValueBo;
import com.erp.gather.domain.vo.ErpCollectProductCategoryValueVo;
import com.erp.gather.mapper.ErpCollectProductCategoryValueMapper;
import com.erp.gather.service.IErpCollectProductCategoryValueService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 泪目valueService业务层处理
 *
 * @author erp
 * @date 2023-04-20
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductCategoryValueServiceImpl implements IErpCollectProductCategoryValueService {

    private final ErpCollectProductCategoryValueMapper baseMapper;

    /**
     * 查询泪目value
     */
    @Override
    public ErpCollectProductCategoryValueVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询泪目value列表
     */
    @Override
    public TableDataInfo<ErpCollectProductCategoryValueVo> queryPageList(ErpCollectProductCategoryValueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductCategoryValue> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductCategoryValueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询泪目value列表
     */
    @Override
    public List<ErpCollectProductCategoryValueVo> queryList(ErpCollectProductCategoryValueBo bo) {
        LambdaQueryWrapper<ErpCollectProductCategoryValue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductCategoryValue> buildQueryWrapper(ErpCollectProductCategoryValueBo bo) {
        LambdaQueryWrapper<ErpCollectProductCategoryValue> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getZhName()), ErpCollectProductCategoryValue::getZhName, bo.getZhName());
        lqw.like(StringUtils.isNotBlank(bo.getEnName()), ErpCollectProductCategoryValue::getEnName, bo.getEnName());
        return lqw;
    }

    /**
     * 新增泪目value
     */
    @Override
    public Boolean insertByBo(ErpCollectProductCategoryValueBo bo) {
        ErpCollectProductCategoryValue add = BeanUtil.toBean(bo, ErpCollectProductCategoryValue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改泪目value
     */
    @Override
    public Boolean updateByBo(ErpCollectProductCategoryValueBo bo) {
        ErpCollectProductCategoryValue update = BeanUtil.toBean(bo, ErpCollectProductCategoryValue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductCategoryValue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除泪目value
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
